package luongvd.weather.model.delegate;

import java.util.ArrayList;
import java.util.List;

import luongvd.weather.common.Const;
import luongvd.weather.common.service.utils.HttpConnectionUtils;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.os.Handler;

public class ServiceDelegate {

	//get location from address
	public static Thread getLocation(final String address, final Handler handler) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(Const.KEY_ADDRESS, address));
		params.add(new BasicNameValuePair(Const.KEY_ADDRESS, "true"));
		final Runnable runnable = new Runnable() {
			public void run() {
				HttpConnectionUtils.getFromURL(Const.URL_GET_LOCATION_FROM_ADDRESS, params, handler, null);
			}
		};
		return HttpConnectionUtils.performOnBackgroundThread(runnable);
	}
	
	//get weather
	public static Thread getWeather(final String lat, final String lon, final Handler handler) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(Const.KEY_LAT, lat));
		params.add(new BasicNameValuePair(Const.KEY_LON, lon));
		params.add(new BasicNameValuePair(Const.KEY_UNITS, Const.KEY_METRIC));
		params.add(new BasicNameValuePair(Const.KEY_MODE, Const.KEY_JSON));
		final Runnable runnable = new Runnable() {
			public void run() {
				HttpConnectionUtils.getFromURL(Const.URL_WEATHER, params, handler, null);
			}
		};
		return HttpConnectionUtils.performOnBackgroundThread(runnable);
	}
	
	//get forecast
		public static Thread getForecast(final String lat, final String lon, final Handler handler) {
			final List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(Const.KEY_LAT, lat));
			params.add(new BasicNameValuePair(Const.KEY_LON, lon));
			params.add(new BasicNameValuePair(Const.KEY_UNITS, Const.KEY_METRIC));
			params.add(new BasicNameValuePair(Const.KEY_MODE, Const.KEY_JSON));
			params.add(new BasicNameValuePair(Const.KEY_NUMBER_DAY_FORECAST, "7"));
			final Runnable runnable = new Runnable() {
				public void run() {
					HttpConnectionUtils.getFromURL(Const.URL_FORECAST, params, handler, null);
				}
			};
			return HttpConnectionUtils.performOnBackgroundThread(runnable);
		}
	
}
